package com.youchuang.action.user;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.annotations.JSON;

import com.opensymphony.xwork2.ActionSupport;
import com.youchuang.action.BaseAction;
import com.youchuang.bean.User;
import com.youchuang.common.Constant;
import com.youchuang.service.UserService;

/**
 * 表单ajax验证
 * 
 * @author Administrator
 *
 */
@Namespace(value = "/user")
@ParentPackage("shopping-default")
public class RegistValiAction extends BaseAction {
	private Logger logger = Logger.getLogger(RegistValiAction.class.getSimpleName());
	private String checkCode;
	private User user;
	private boolean flag;

	@Resource
	private UserService userService;

	@Action(value = "valiCheckCodeAction", results = { @Result(name = ActionSupport.SUCCESS, type = "json") })
	public String valiCheckCode() {
		logger.info("method is valiCheckCode");
		if (checkCode != null && checkCode.equalsIgnoreCase(session.get(Constant.CHECK_CODE).toString())) {
			flag = true;
		} else {
			flag = false;
		}
		return Constant.SUCCESS;
	}

	@Action(value = "valiEmailAction", results = { @Result(name = ActionSupport.SUCCESS, type = "json") })
	public String valiEmail() throws Exception {
		logger.info("method is valiEmail");
		User u = userService.findUserByEmail(user.getEmail());
		if (u != null) {
			flag = false;
		} else {
			flag = true;
		}
		return Constant.SUCCESS;
	}

	@JSON(serialize = false)
	public String getCheckCode() {
		return checkCode;
	}

	public void setCheckCode(String checkCode) {
		this.checkCode = checkCode;
	}

	@JSON(serialize = false)
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public boolean isFlag() {
		return flag;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}

}
